
'use strict';

const Controller = require('egg').Controller;

class RoleController extends Controller {

  constructor(ctx) {
    super(ctx);

    this.createRule = {
      name: { type: 'string', required: true, allowEmpty: false },
    };
  }

  async create() {
    const { ctx, service } = this;
    const payload = ctx.request.body || {}
    let errs = this.app.validator.validate(rule, payload);
    if (errs !== undefined) {
      const err = { err: errs };
      ctx.helper.failure({ ctx, err });
      return;
    }
    const res = await service.role.create(payload);
    ctx.helper.success({ ctx, res });
  }

  async destroy() {
    const { ctx, service } = this;
    const { id } = ctx.params;
    await service.role.destroy(id);
    ctx.helper.success({ ctx });
  }

  async update() {
    const { ctx, service } = this;
    ctx.validate(this.createRule);
    const { id } = ctx.params;
    const payload = ctx.request.body || {};
    await service.role.update(id, payload);
    ctx.helper.success({ ctx });
  }

  async show() {
    const { ctx, service } = this;
    ctx.validate(this.createRule);
    const { id } = ctx.params;
    const res = await service.role.show(id)
    ctx.helper.success({ ctx, res });
  }

  async index() {
    const { ctx, service } = this
    const payload = ctx.query
    const res = await service.role.index(payload)
    ctx.helper.success({ ctx, res })
  }
}

module.exports = RoleController;
